package mdsd2_team3betportalmodel.generator.model.ui;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.jboss.tools.seam.internal.core.project.facet.ISeamFacetDataModelProperties;
import org.jboss.tools.seam.ui.SeamUIMessages;
import org.jboss.tools.seam.ui.internal.project.facet.SeamInstallWizardPage;
import org.jboss.tools.seam.ui.wizard.SeamProjectWizard;

import template.XPandRunner;
import betportal.Portal;

@SuppressWarnings("restriction")
public class GenerateAction implements IObjectActionDelegate {

	private Portal portal;

	public GenerateAction() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run(IAction action) {
		SeamProjectWizard wizard = new SeamProjectWizard();
		wizard.init(PlatformUI.getWorkbench(), null);
		WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
		dialog.open();
		
		if (dialog.getReturnCode() == Dialog.OK) {
			
			SeamInstallWizardPage page = (SeamInstallWizardPage) wizard.getPage(SeamUIMessages.SEAM_INSTALL_WIZARD_PAGE_SEAM_FACET);
			String ejbProjectName = page.getConfig().getStringProperty(ISeamFacetDataModelProperties.SEAM_EJB_PROJECT);
			String earProjectName = page.getConfig().getStringProperty(ISeamFacetDataModelProperties.SEAM_EAR_PROJECT);
			
			copyJARs(ejbProjectName, earProjectName);
						
			XPandRunner gen = new XPandRunner();
			
			try {
				gen.generateProject(portal, ejbProjectName, earProjectName);
			} 
			catch (CoreException e) {
				e.printStackTrace();
			}
		}
		else if (dialog.getReturnCode() == Dialog.CANCEL) {
			InputDialog id = new InputDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Input text", "Please provide the name of the EJB project!", null, null);
			id.open();
			if (id.getReturnCode() == Dialog.OK) {
				System.out.println("Generate");
				XPandRunner gen = new XPandRunner();
				
				try {
					gen.generateProject(portal, id.getValue(), "");
				} 
				catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			this.portal = (Portal) ((IStructuredSelection) selection).getFirstElement();
		}
		
	}
	
	private void copyJARs(String ejbProjectName, String earProjectName) {
		IWorkspace ws = ResourcesPlugin.getWorkspace();
		IProject earProject = ws.getRoot().getProject(earProjectName);
		String earProjectPath = earProject.getLocation().toOSString()+"\\EarContent\\lib\\";
		
		IProject ejbProject = ws.getRoot().getProject(ejbProjectName);
		String ejbProjectPath = ejbProject.getLocation().toOSString()+"\\ejbModule\\";

		IProject generatorProject = ws.getRoot().getProject("MDSD2_Team3BetPortalModel");
		String generatorProjectPath = generatorProject.getLocation().toOSString()+"\\resources\\";
		
		System.out.println("gen path = "+generatorProjectPath);
		System.out.println("ejb "+ejbProjectPath);
		System.out.println("ear "+earProjectPath);
		
		String[] fileNames = new String[4];
		fileNames[0] = "entities.jar";
		fileNames[1] = "jaxrs-api.jar";
		fileNames[2] = "jboss-seam-resteasy.jar";
		fileNames[3] = "resteasy-jaxrs.jar";
		
		copy(generatorProjectPath+fileNames[0], ejbProjectPath+fileNames[0]);
		
		for (int i=1;i<=3;i++) {
			copy(generatorProjectPath+fileNames[i], earProjectPath+fileNames[i]);
		}
		
	}

	private void copy(String file1, String file2) {
		try {
			InputStream in = new FileInputStream(file1);
			OutputStream out = new FileOutputStream(file2);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
	
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub

	}

}
